/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package medialibrary.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.persistence.Query;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.transaction.UserTransaction;
import medialibrary.persistence.Oeuvre;

/**
 *
 * @author user
 */
@WebServlet(name="MemberUIServlet", urlPatterns={"/MemberUI"})
public class MemberUIServlet extends HttpServlet {

    @PersistenceUnit
    //The emf corresponding to 
    private EntityManagerFactory emf;  
    
    @Resource
    private UserTransaction utx;
    
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        assert emf != null;  //Make sure injection went through correctly.
        EntityManager em = null;
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        try{    
            if(session.getAttribute("usertype")!="user"){
              response.sendRedirect(response.encodeRedirectURL("/MediaLibrary/disconnect.jsp") );
            }
        }catch(Exception e){}
        out.println("<html>"); 
        out.println("<head>"); 
        out.println("<title>Bienvenue dans Mediathèque</title>"); 
        out.println("<link rel='stylesheet' href='css/flexi-background.css' type='text/css' media='screen' />"); 
        out.println("<link rel='stylesheet' href='css/styles.css' type='text/css' media='screen' />"); 
        out.println("</head>"); 
        out.println("<body>"); 
        
        out.println("<script src='js/flexi-background.js' type='text/javascript' charset='utf-8'></script>"); 
        out.println("<img src='images/Polytech_logo.png' class='polytechlogo' alt='yourlogo' />	"); 
        out.println("<div id='box' style='margin: 16% 0% 0;float:left;'>"); 
        out.println("<img src='images/logo.png' class='logo' alt='yourlogo' />"); 
        out.println("                "); 
        out.println("<h1>Chercher un Ouvrages</h1>");
        out.println("<form action=\"/MediaLibrary/SearchServlet\">");

        out.println("<input type='text' name='word' onclick='this.value='';' onfocus='this.select()' onblur='this.value=!this.value?'Mot clef':this.value;' value='Mot clef' />"); 
        out.println("<input type='submit' name='' value='Chercher' style='top: 223px;left: 149px;' />"); 
        out.println("<input type='button' name='' value='mes emprunts' style='top: 423px;left: 149px;' onClick='window.location=\"BorrowingServlet?memberid="+session.getAttribute("id") +"\"'/>");
                out.println("<input type='button' name='' value='mes reservations' style='top: 373px;left: 149px;' onClick='window.location=\"ReservationServlet?memberid="+session.getAttribute("id") +"\"'/>"); 
        out.println("<input type='button' name='' value='se déconnecter' style='top: 473px;left: 149px;' onClick='window.location=\"disconnect.jsp\"' />"); 
        out.println("</div>"); 
        out.println("<div id='catalogue' class='alpha60'>"); 
        out.println("    <h1 style='width: 365px;position: static;'>Catalogue des Ouvrages</h1>"); 
 
        try {
            utx.begin();
            
            em = emf.createEntityManager();
            Query query = em.createQuery("select o from Oeuvre o");
            
            List<Oeuvre> works = query.getResultList();
            
            if (works.size() > 0) {
                   for(Oeuvre e:works) {
                        out.println("    <div class='imgbox' id='imgbox1'>"); 
                        out.println("<h3>"+e.getTypeName()+" : "+e.getTitle()+"</h3>"); 
                        out.println("<h4>Auteur : "+e.getAuthor()+"</h4>"); 
                        out.println("<h4>Genre : "+e.getGenre()+"</h4>"); 
                        out.println("<h4>ISBN : "+e.getOeuvreISBN()+"</h4>"); 
                        out.println("<input type='button' value='réservez' onClick='window.location=\"BorrowReserveServlet?isbn="+e.getOeuvreISBN()+"&memberid="+session.getAttribute("id") +"\"'/>"); 
                        out.println("    </div>");                        
                   }
                   
            } else {
                out.println("Aucun ouvrage n'est disponible pour le moment");
            }
            out.println("</div>"); 
            out.println("	"); 
            out.println("    </body>"); 
            out.println("</html>");
            
            
            
            utx.commit();
        } catch (Exception e) {
            throw new ServletException(e);
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
